package sddz.foodmanage.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatDelegate;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import sddz.foodmanage.dialog.BindCardDiaLog;


/**
 * Created by zhanglei on 2017/6/27.
 */

public  class BaseFragment extends Fragment {

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    protected Activity mActivity;
    protected Fragment mFragment;
    protected ProgressDialog baseDialog;
    protected Handler baseHandler = new Handler();
    private BindCardDiaLog cardDiaLog;
//    protected  boolean isVisible;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
        mFragment = this;
        baseDialog = new ProgressDialog(mActivity);
        baseDialog.setMessage("加载中...");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        cardDiaLog = new BindCardDiaLog(mActivity);
        return super.onCreateView(inflater, container, savedInstanceState);

    }

    protected void show(String message) {
        Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
    }

//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        if(getUserVisibleHint()){
//            isVisible=true;
//            onVisible();
//        }else{
//            isVisible=false;
//            onVisible();
//        }
//    }
//    protected void onVisible(){
//        lazyLoad();
//    }
//    protected  void onInvisible(){
//
//    }
//    protected abstract  void lazyLoad();
    //    protected void initProgress(TwinklingRefreshLayout layout) {
//        ProgressLayout progressLayout = new ProgressLayout(mActivity);
//        progressLayout.setColorSchemeResources(R.color.colorPrimary);
//        layout.setHeaderView(progressLayout);
//    }
//
//    protected void popFragment() {
//        if (mActivity instanceof OpenFragmentActivity) {
//            mActivity.finish();
//            return;
//        }
//        getFragmentManager().popBackStack();
//    }

    /**
     * 获取String类型的返回结果
     *
     * @param data
     * @param callback
     */
//    public void postString(String data, String url, final OnHttpCallback callback) {

//        HttpUtils.getInstance().POST(mActivity, data, url, new HttpUtils.OnOkHttpCallback() {
//            @Override
//            public void onSuccess(String body) {
//                if (callback != null) {
//                    try {
//                        JSONObject object = new JSONObject(body);
//                        callback.onSuccess(object);
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//
//            @Override
//            public void onError(Request error, Exception e) {
//                if (callback != null) {
//                    callback.onError(error, e);
//                }
//            }
//        });

//    }
//
//    /**
//     * 根据泛型获取返回结果
//     *
//     * @param data
//     * @param callName
//     * @param httpResult
//     * @param <T>
//     */
//    public <T> void getHttpContent(String data, String callName, final HttpResult<T> httpResult) {
//
//    }

//    public interface OnHttpCallback {
//        void onSuccess(JSONObject object);
//
//        void onError(Request error, Exception e);
//    }

    /**
     * 返回Json对象
     *
     * @param data
     * @return
     * @throws JSONException
     */
    protected JSONObject getJson(Map<String, String> data) {
        return new JSONObject(data);
    }

    /**
     * 返回Json对象
     *
     * @param data
     * @return
     * @throws JSONException
     */
    protected JSONObject getJson(String data) {
        try {
            return new JSONObject(data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void isBindCard() {
//        UserBean userData = SpfUtils.getUserData(mActivity);
//        if (userData.isBind()) {
//            show("此功能暂时为开放");
//            return true;
//        } else {
        cardDiaLog.showDiaLog();
//            return false;
//        }
    }
}
